package com.frank.easyexcel.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.frank.easyexcel.exportManyTableSheet.StudentDto;
import com.frank.easyexcel.listener.ExcelListener;
import com.frank.easyexcel.model.PersonInExcel;
import com.frank.easyexcel.model.dto.PersonDto;
import com.frank.easyexcel.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 小石潭记
 * @date 2020/10/7 20:01
 * @Description: ${todo}
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Value("${upload.maxLine}")
    private Integer maxLine;

    /**
     * 导入数据
     * @param file
     */
    @PostMapping(value = "upload")
    public List<PersonInExcel> importExcel(@RequestParam("file") MultipartFile file){
        try{
            InputStream inputStream = file.getInputStream();
            //实例化实现了AnalysisEventListener接口的类，
            ExcelListener listener = new ExcelListener(maxLine);
            EasyExcel.read(inputStream, PersonInExcel.class, listener).sheet().doRead();
            //获取数据
            List<PersonInExcel> list = listener.getDatas();
            System.out.println(JSON.toJSON(list));
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 下载模板
     */
    @GetMapping(value = "/down")
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<PersonDto> list = new ArrayList<>();
            PersonDto personDto = new PersonDto();
            personDto.setId("1");
            personDto.setName("测试");
            personDto.setBirth(LocalDateTime.now());
            personDto.setCreateDate(LocalDate.now());
            list.add(personDto);
            ExcelUtil.writeExcel(response, list, "信息", "Sheet1", PersonDto.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载模板
     */
    @GetMapping(value = "/down-many")
    public void downloadManyExcel(HttpServletRequest request, HttpServletResponse response) {
        /*ExcelWriter excelWriter = EasyExcel.write("多表导出").build();
        List<StudentDto> studentList=new ArrayList<>();
        StudentDto student=new StudentDto("1","张三","2000-01-01");
        studentList.add(student);
        //这里 需要指定写用哪个class去写
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "学生信息1").head(StudentDto.class).build();
        excelWriter.write(studentList, writeSheet);
        writeSheet = EasyExcel.writerSheet(1, "学生信息2").head(StudentDto.class).build();
        excelWriter.write(studentList, writeSheet);
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();*/
    }

    /**
     * 导出多个sheet
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception{
        String fileName="报表";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        List<StudentDto> studentList=new ArrayList<>();
        List<StudentDto> studentList1=new ArrayList<>();
        StudentDto student=new StudentDto("1","张三","2000-01-01");
        StudentDto student1=new StudentDto("2","李四","2000-01-01");
        studentList.add(student);
        studentList1.add(student1);
        //这里 需要指定写用哪个class去写
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "学生信息1").head(StudentDto.class).build();
        excelWriter.write(studentList, writeSheet);
        writeSheet = EasyExcel.writerSheet(1, "学生信息2").head(StudentDto.class).build();
        excelWriter.write(studentList1, writeSheet);
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }


}
